import { PostOrder } from "../../api/orderw";
import { GetGood } from "../../api/good3c";
import { err } from "../../utils/console";
import { toast } from "../../utils/wxapi";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    description: "",
    model_name: "",
    brand_name: "",
    address_id: 1,
    pic: [],
    page: 1,
    limit: 20,
    list: [] as any,
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getlist();
  },
  img(e: any) {
    console.log(e);
    const { src } = e.currentTarget.dataset;
    console.log(src);
    wx.previewImage({
      current: src,
      urls: [src],
    });
  },
  async getlist() {
    try {
      const { page, limit } = this.data;
      const res = await GetGood({
        page,
        limit,
      });
      console.log(res);
      if (res.statusCode === 200) {
        const newList = res.data;
        const updatedList = [...this.data.list, ...newList];
        this.setData({
          list: updatedList,
          page: this.data.page + 1,
          isLoading: false,
        });
      }
    } catch (error) {
      err(error);
    }
  },
  postw() {
    const { description, model_name, brand_name, address_id } = this.data;
    const pic = [
      "cloud://prod-3gkvcu2a4ef45fe7.7072-prod-3gkvcu2a4ef45fe7-1319308337/good_img/1690174477222.jpg",
    ];
    const data = { description, model_name, brand_name, address_id, pic };

    PostOrder(data)
      .then((res: any) => {
        console.log(res);
        if (res.statusCode === 200) {
          toast("提交成功");
        }
      })
      .catch((error: any) => {
        err(error);
      });
  },
  toimg() {
    wx.showLoading({
      title: "上传中",
    });
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album", "camera"],
      success: (res) => {
        console.log(res);
        let head = res.tempFiles[0].tempFilePath;
        let timestamp = new Date().valueOf();

        wx.cloud.uploadFile({
          cloudPath: "good_img/" + timestamp + ".jpg",
          filePath: head,
          success: (res) => {
            console.log(res);
            toast("上传成功");
          },
          fail: () => {
            toast("上传失败");
          },
        });
      },
      fail: () => {
        toast("上传失败");
        wx.hideLoading();
      },
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
